﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication;
using HslCommunication.Profinet.Siemens;
using Models;

namespace BLL
{
    /// <summary>
    /// S7西门子读写类
    /// </summary>
    class S71200Helper
    {
        #region 读
        public static object S7Read(SiemensS7Net PlcDrive, string IOAdress, PLCDataType IODataType)
        {
            switch (IODataType)
            {
                case PLCDataType.PLCBool:
                    bool res1 = PlcDrive.ReadBool(IOAdress).Content;
                    return res1;
                case PLCDataType.PLCByte:
                    byte res2 = PlcDrive.ReadByte(IOAdress).Content;
                    return res2;
                case PLCDataType.PLCInt16:
                    short res3 = PlcDrive.ReadInt16(IOAdress).Content;
                    return res3;
                case PLCDataType.PLCUInt16:
                    ushort res4 = PlcDrive.ReadUInt16(IOAdress).Content;
                    return res4;
                case PLCDataType.PLCInt32:
                    int res5 = PlcDrive.ReadInt32(IOAdress).Content;
                    return res5;
                case PLCDataType.PLCUInt32:
                    uint res6 = PlcDrive.ReadUInt32(IOAdress).Content;
                    return res6;
                case PLCDataType.PLCFloat:
                    float res7 = PlcDrive.ReadFloat(IOAdress).Content;
                    return res7;
                case PLCDataType.PLCInt64:
                    long res8 = PlcDrive.ReadInt64(IOAdress).Content;
                    return res8;
                case PLCDataType.PLCUInt64:
                    ulong res9 = PlcDrive.ReadUInt64(IOAdress).Content;
                    return res9;
                case PLCDataType.PLCDouble:
                    double res10 = PlcDrive.ReadDouble(IOAdress).Content;
                    return res10;
                case PLCDataType.PLCString:
                    string res11 = PlcDrive.ReadString(IOAdress, 10).Content;
                    return res11;
            }
            return null;
        }
        #endregion

        #region 写
        public static bool S7Write(SiemensS7Net PlcDrive, string IOAdress, object IOValue, PLCDataType IODataType)
        {
            try
            {
                switch (IODataType)
                {
                    case PLCDataType.PLCBool:
                        PlcDrive.Write(IOAdress, (bool)IOValue);
                        break;
                    case PLCDataType.PLCByte:
                        PlcDrive.Write(IOAdress, (byte)IOValue);
                        break;
                    case PLCDataType.PLCInt16:
                        PlcDrive.Write(IOAdress, (short)IOValue);
                        break;
                    case PLCDataType.PLCUInt16:
                        PlcDrive.Write(IOAdress, (ushort)IOValue);
                        break;
                    case PLCDataType.PLCInt32:
                        PlcDrive.Write(IOAdress, (int)IOValue);
                        break;
                    case PLCDataType.PLCUInt32:
                        PlcDrive.Write(IOAdress, (uint)IOValue);
                        break;
                    case PLCDataType.PLCFloat:
                        PlcDrive.Write(IOAdress, (float)IOValue);
                        break;
                    case PLCDataType.PLCInt64:
                        PlcDrive.Write(IOAdress, (long)IOValue);
                        break;
                    case PLCDataType.PLCUInt64:
                        PlcDrive.Write(IOAdress, (ulong)IOValue);
                        break;
                    case PLCDataType.PLCDouble:
                        PlcDrive.Write(IOAdress,(double)IOValue);
                        break;
                    case PLCDataType.PLCString:
                        PlcDrive.Write(IOAdress, IOValue.ToString());
                        break;
                    default:
                        break;
                }
                return true;
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        #endregion
    }
}
